import unittest, logging
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait  # 显示等待的包
from selenium.webdriver.support import expected_conditions as EC  # 预期判断条件的包
from ui_zdh.baidu_ui.Tools import Tools

class Baidu_unittest(unittest.TestCase):
    def setUp(self) -> None:
        Tools().Log()
        # 打开浏览器
        self.driver = webdriver.Firefox()
        # 双击打开百度页面
        self.driver.get('https://www.baidu.com/')
        # 隐式等待：全局性 每次查找元素时都会使用这个等待时间  适用于整个 WebDriver 的生命周期，不需要针对每个元素进行单独配置  查询一次
        self.driver.implicitly_wait(5)
        # 窗口最大化
        self.driver.maximize_window()
        time.sleep(2)

    def tearDown(self) -> None:
        time.sleep(5)
        self.driver.quit()

    def test_baidu_01(self):
        logging.info('start 01 case')
        # 显示等待   局部   显示等待是对某个具体的元素进行等待指定时间，直到某个条件被满足
        seatch = WebDriverWait(self.driver, 20, 0.5).until(
            EC.presence_of_element_located((By.ID, 'kw'))
        )
        # 键盘输入事件
        seatch.send_keys('selenium键盘鼠标浏览器事件')
        # 强制等待
        time.sleep(2)
        # 键盘回车事件
        seatch.send_keys(Keys.ENTER)
        time.sleep(2)
        logging.info('01 case end')

    def test_baidu_02(self):
        logging.info('start 02 case')
        self.driver.find_element(By.CSS_SELECTOR, '.soutu-btn').click()
        time.sleep(2)
        # 上传图片
        self.driver.find_element(By.CLASS_NAME, 'upload-pic').send_keys(r'C:\Users\Lenovo\Desktop\天伟\4.jpg')
        time.sleep(5)
        logging.info('02 case end')

    def test_baidu_03(self):
        logging.info('start 03 case')
        pass
        logging.info('03 case end')



if __name__ == '__main__':
    unittest.main()